<?php

namespace App\Observers;

use App\Models\Voucher;
use Auth;

class VoucherObserver
{


    public function created(Voucher $voucher)
    {

        if ($voucher->judge())
            $record = $voucher->records()->create([
                'user_id' => $voucher->user_id,
                'type' => 'donate',
            ]);
    }


    public function updated(Voucher $voucher)
    {

//        需要判断是不是兑换动作

//改变状态

        $type = 'donate';
        if ($voucher->status == 3) {
            $type = 'writeOff';
        }
        $voucher->records()->create([
            'user_id' => Auth::id(),
            'voucher_id' => $voucher->uuid,
            'type' => $type,
            'currentTimes' => $voucher->times,
        ]);
    }


    public function deleted(Voucher $voucher)
    {
        //
    }


    public function restored(Voucher $voucher)
    {
        //
    }


    public function forceDeleted(Voucher $voucher)
    {
        //
    }
}
